package com.lxmes.business.service.impl;

import java.util.List;

import com.lxmes.common.annotation.DataScope;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.business.mapper.CcInventoryRulesMapper;
import com.lxmes.business.domain.CcInventoryRules;
import com.lxmes.business.service.ICcInventoryRulesService;

/**
 * 盘点规则Service业务层处理
 *
 * @author lx
 * @date 2023-06-29
 */
@Service
public class CcInventoryRulesServiceImpl implements ICcInventoryRulesService
{
    @Autowired
    private CcInventoryRulesMapper ccInventoryRulesMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询盘点规则
     *
     * @param id 盘点规则主键
     * @return 盘点规则
     */
    @Override
    public CcInventoryRules selectCcInventoryRulesById(Long id)
    {
        return ccInventoryRulesMapper.selectCcInventoryRulesById(id);
    }

    /**
     * 查询盘点规则列表
     *
     * @param ccInventoryRules 盘点规则
     * @return 盘点规则
     */
    @DataScope(deptAlias = "de", userAlias = "de")
    @Override
    public List<CcInventoryRules> selectCcInventoryRulesList(CcInventoryRules ccInventoryRules)
    {
        return ccInventoryRulesMapper.selectCcInventoryRulesList(ccInventoryRules);
    }

    /**
     * 新增盘点规则
     *
     * @param ccInventoryRules 盘点规则
     * @return 结果
     */
    @Override
    public int insertCcInventoryRules(CcInventoryRules ccInventoryRules)
    {
        ccInventoryRules.setCreateTime(DateUtils.getNowDate());
        ccInventoryRules.setDeptId(SecurityUtils.getDeptId());
        return ccInventoryRulesMapper.insertCcInventoryRules(ccInventoryRules);
    }

    /**
     * 修改盘点规则
     *
     * @param ccInventoryRules 盘点规则
     * @return 结果
     */
    @Override
    public int updateCcInventoryRules(CcInventoryRules ccInventoryRules)
    {
        ccInventoryRules.setUpdateTime(DateUtils.getNowDate());
        return ccInventoryRulesMapper.updateCcInventoryRules(ccInventoryRules);
    }

    /**
     * 批量删除盘点规则
     *
     * @param ids 需要删除的盘点规则主键
     * @return 结果
     */
    @Override
    public int deleteCcInventoryRulesByIds(Long[] ids)
    {
        return ccInventoryRulesMapper.deleteCcInventoryRulesByIds(ids);
    }

    /**
     * 删除盘点规则信息
     *
     * @param id 盘点规则主键
     * @return 结果
     */
    @Override
    public int deleteCcInventoryRulesById(Long id)
    {
        return ccInventoryRulesMapper.deleteCcInventoryRulesById(id);
    }
    @Override
    public String imporIteminfo(List<CcInventoryRules> ccInventoryRulesList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ccInventoryRulesList) || ccInventoryRulesList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CcInventoryRules ccInventoryRules : ccInventoryRulesList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ccInventoryRules);
                ccInventoryRules.setCreateBy(operName);
                ccInventoryRules.setUpdateBy(operName);
                this.insertCcInventoryRules(ccInventoryRules);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
